#include "ui_message_handler.h"
#include "control.h"
#include "ui_message.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

ThreadSafeUIMessageQueue g_ui_message_queue; // 全局线程安全消息队列

extern lv_obj_t *ui_wendu1; // 声明UI温度控件

void ui_message_system_init(size_t queue_capacity)
{
    ui_message_queue_init(&g_ui_message_queue, queue_capacity);
    // 设置默认容量如果未指定
    if (queue_capacity == 0)
    {
        queue_capacity = 32; // 默认消息队列大小
    }
}

void ui_message_system_destroy()
{
    ui_message_queue_destroy(&g_ui_message_queue);
}

void ui_message_process_all()
{
    UIMessageBase *msg;
    while ((msg = ui_message_queue_try_pop(&g_ui_message_queue)) != NULL)
    {

        switch (msg->type)
        {
        case MSG_UPDATE_LABEL_TEXT:  // 更新标签文本
        {
            UILabelUpdateMsg *label_msg = (UILabelUpdateMsg *)msg;
            if (label_msg->base.target && lv_obj_is_valid(label_msg->base.target))
            {
                lv_label_set_text(label_msg->base.target, label_msg->text);
            }
            break;
        }
        case MSG_UPDATE_LABEL_STYLE:  // 更新标签文本
        {
            UIStyleUpdateMsg *style_msg = (UIStyleUpdateMsg *)msg;
            if (style_msg->base.target && lv_obj_is_valid(style_msg->base.target) && style_msg->style != NULL)
            {
                lv_obj_add_style(style_msg->base.target, style_msg->style, style_msg->part | style_msg->state);
            }
            else
            {
                if (!style_msg->style)
                    log_message(1, "Invalid style pointer in MSG_UPDATE_LABEL_STYLE");
            }
            break;
        }
        case MSG_ASYNC_CALLBACK:  // 执行异步回调
        {
            UIAsyncCallbackMsg *callback_msg = (UIAsyncCallbackMsg *)msg;
            if (callback_msg->callback)
            {
                callback_msg->callback(callback_msg->user_data);
            }
            break;
        }
        // ============= 新增天气温度消息处理 =============
        case MSG_UPDATE_WEATHER_TEMP:   // 更新温度显示
            printf("INFO: 接收到天气温度消息 - 目标控件: %p, 本地ui_wendu1: %p\n", msg->target, ui_wendu1);
            if (msg->target == ui_wendu1 && lv_obj_is_valid(ui_wendu1))
            {
                lv_label_set_text(ui_wendu1, ((UIWeatherTempMsg *)msg)->value);
                printf("INFO: UI温度已更新: %s\n", ((UIWeatherTempMsg *)msg)->value);
            }
            else
            {
                printf("ERROR: 无效的ui_wendu1控件或消息目标不匹配\n");
            }
            break;
        }

        if (msg->free_fn)
        {
            msg->free_fn(msg);
        }
        else
        {
            free(msg);
        }
    }
}

void safe_label_set_text(lv_obj_t *label, const char *text)
{
    UILabelUpdateMsg *msg = create_label_update_msg(label, text);
    ui_message_queue_push(&g_ui_message_queue, (UIMessageBase *)msg);
}

void safe_obj_add_style(lv_obj_t *obj, lv_style_t *style, lv_part_t part, lv_state_t state)
{
    UIStyleUpdateMsg *msg = create_style_update_msg(obj, style, part, state);
    ui_message_queue_push(&g_ui_message_queue, (UIMessageBase *)msg);
}

void safe_async_call(void (*callback)(void *), void *user_data)
{
    UIAsyncCallbackMsg *msg = create_async_callback_msg(callback, user_data);
    ui_message_queue_push(&g_ui_message_queue, (UIMessageBase *)msg);
}

// 新增安全温度更新函数
void safe_update_temperature(lv_obj_t *target, const char *temp_value)
{
    UIWeatherTempMsg *msg = malloc(sizeof(UIWeatherTempMsg));
    if (!msg)
    {
        log_message(LOG_ERROR, "分配温度消息内存失败");
        return;
    }

    msg->base.type = MSG_UPDATE_WEATHER_TEMP;
    msg->base.target = target;
    msg->base.free_fn = NULL; // 使用默认free

    // 安全复制温度值
    strncpy(msg->value, temp_value, sizeof(msg->value) - 1);
    msg->value[sizeof(msg->value) - 1] = '\0'; // 确保终止符

    if (!ui_message_queue_push(&g_ui_message_queue, (UIMessageBase *)msg))
    {
        free(msg);
        log_message(LOG_WARNING, "消息队列已满，丢弃温度更新");
    }
}

ThreadSafeUIMessageQueue *get_ui_message_queue(void)
{
    return &g_ui_message_queue;
}
